//
//  AppDelegate.m
//  iMoive
//
//  Created by 侯伟 on 2021/8/24.
//

#import "AppDelegate.h"
#import "NetworkConfig.h"
#import "IdfaTool.h"

#import "AppDelegate+Init.h"
#import "AppDelegate+DB.h"

#import "RCacheManager.h"

#import "RVersionCheckRequest.h"
#import "RVersionUpdateView.h"

#import "RMainViewController.h"
#import "XMainViewController.h"

#import "RootNavigationViewController.h"

@interface AppDelegate ()


@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginAAction:) name:kAdCompleteNotification object:NULL];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginBAction:) name:kChangeNotification object:NULL];
    
    //网络配置
    [NetworkConfig configNetwork];
    
    
    // SKD初始化
    [self configSDKs:launchOptions];
    
    //数据库迁移
    [self migrationRealmAction];
    
    
    UIWindow* window = [[UIWindow alloc] initWithFrame:SCREEN_BOUNDS];
    window.backgroundColor = UIColor.whiteColor;
    self.window = window;
    
    UIViewController* launchScreen = [UIViewController viewControllerWithIdentifier:@"LaunchViewController" storyboardName:@"LaunchScreen"];
    RootNavigationViewController* rootController = [[RootNavigationViewController alloc] initWithRootViewController:launchScreen];
    self.window.rootViewController = rootController;
    [self.window makeKeyAndVisible];
    self.rootController = rootController;
    
    //版本检查
    [Queue delay:3.0 task:^{
        [self checkVersion];
    }];
    
    return YES;
}

/// 进入前台
/// @param application -
- (void)applicationWillEnterForeground:(UIApplication *)application{
    [RCacheManager.share startAllCache];
}

/// 进入后台
/// @param application -
- (void)applicationDidEnterBackground:(UIApplication *)application{
    [RCacheManager.share stopAllCache];
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
    [IdfaTool requestIDFA];
}


// 在这里写支持的旋转方向，为了防止横屏方向，应用启动时候界面变为横屏模式
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // 可以这么写
    if (self.allowOrentitaionRotation) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark  -- methods

///版本检查
- (void)checkVersion{
    RVersionCheckRequest* request = [RVersionCheckRequest new];
    request.v = [HApp version];
    [RequestOperation request:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(RUpdateInfo*  _Nonnull data) {
            if(data.update){
                RVersionUpdateView* versionView = [RVersionUpdateView share];
                versionView.updateInfo = data;
                [self.window addSubview:versionView];
            }
        }];
    }];
}
#pragma mark  -- notification

/// A
/// @param notification -
- (void)loginAAction:(NSNotification*)notification{

    if([self.rootController.viewControllers.firstObject isKindOfClass:XMainViewController.class]){
        return;
    }
    XMainViewController* tabbarController = [XMainViewController new];
    [self.rootController setViewControllers:@[tabbarController] animated:NO];
    [self.window makeKeyAndVisible];
}

/// B
/// @param notification -
- (void)loginBAction:(NSNotification*)notification{
    
    if([self.rootController.viewControllers.firstObject isKindOfClass:RMainViewController.class]){
        return;
    }
    RMainViewController* tabbarController = [RMainViewController new];
    [self.rootController setViewControllers:@[tabbarController] animated:NO];
    [self.window makeKeyAndVisible];
}

@end
